﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HotelBookingSystem
{
    public class Clients : IClient
    {
        private ArrayList clients;

        public Clients()
        {
            clients = new ArrayList();
            RegisterClient("Patrick", "Duffy", "patrick@gmail.com");
        }

        public int RegisterClient(string FirstName, string LastName, string EmailAddress)
        {
            Client client = new Client(FirstName, LastName, EmailAddress);
            clients.Add(client);
            return client.ClientId;
        }

        public void UnregisterClient(int id)
        {
            Client client = new Client(id);
            clients.Remove(client);
        }

        public void ShowClients(int id)
        {

            if (!CheckId(id) && id != -1)
                return;

            foreach (Client client in clients)
            {
                if(id == -1 && id == client.ClientId)
                {
                string sid = client.ClientId.ToString().PadLeft(4);
                string fname = client.FirstName.PadRight(12);
                string lname = client.LastName.PadRight(12);
                string email = client.EmailAddress.PadRight(20);

                string str = sid + " " + fname + " " + lname + " " + email;
                Console.WriteLine(str);
                }
            }
        }

        public void ChangeEmailAddress(int id, string emailAddress)
        {
            try
            {
                foreach (Client client in clients)
            {
                if (client.ClientId == id)
                {
                    client.EmailAddress = emailAddress;
                    return;
                } 
                    else
                    {
                        throw new NotFoundException();
                    }
                }
            }
            catch (NotFoundException)
            {

                Console.WriteLine("id" + id + "not found");
            }   
        }

        private bool CheckId(int id)
        {
            Client client = new Client(id);
            return clients.Contains(client);
        }

        public ArrayList GetClient(int id)
        {
            ClientListItem item;
            ArrayList list = new ArrayList();
            if (!CheckId(id) && id != -1)
                return list;
            foreach (Client cust in clients)
            {
                if (id == -1 || id == cust.ClientId)
                {
                    item.ClientId = cust.ClientId;
                    item.FirstName = cust.FirstName;
                    item.LastName = cust.LastName;
                    item.EmailAddress = cust.EmailAddress;
                    list.Add(item);
                }
            }
            return list;
        }
    }
}
